package com.newmacys.tiles;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.tiles.TilesApplicationContext;
import org.apache.tiles.context.TilesRequestContextFactory;
import org.apache.tiles.definition.DefinitionsFactoryException;
import org.apache.tiles.factory.AbstractTilesContainerFactory;
import org.apache.tiles.factory.BasicTilesContainerFactory;
import org.apache.tiles.startup.AbstractTilesInitializer;
import org.apache.tiles.startup.TilesInitializer;
import org.apache.tiles.web.startup.AbstractTilesInitializerServlet;

public class TilesServlet extends AbstractTilesInitializerServlet {

	@Override
	protected TilesInitializer createTilesInitializer() {
		return new TilesInitializer();
	}

    private static class TilesInitializer extends AbstractTilesInitializer {

		@Override
		protected AbstractTilesContainerFactory createContainerFactory(
				TilesApplicationContext arg0) {

			return new CustomTilesContainerFactory();
		}
    }

	private static class CustomTilesContainerFactory extends BasicTilesContainerFactory {

		protected List<URL> getSourceURLs(TilesApplicationContext applicationContext, TilesRequestContextFactory contextFactory) {
			try {
		
				List<URL> tilesUrls = new ArrayList<URL>();
				System.out.println("*************Loading tiles configuration*******************");
				tilesUrls.add(applicationContext.getResource("/WEB-INF/tiles.xml"));

				return tilesUrls;
		
			} catch (IOException e) {
				throw new DefinitionsFactoryException(
						"Cannot load tiles definition URLs", e);
			}
		
		}
	
	}

    
}
